$(
    function(){
        findById()
    }

)


layui.use(['form', 'layer', 'jquery', 'laydate'],function () {


    let laydate = layui.laydate;
    let $ = layui.jquery;
    let form = layui.form;
    let layer = layui.layer;

    laydate.render(
        {elem: '#L_birthday'}
    );
    form.verify(
        {
            username: [/^[\w-]{4,16}$/, "你输入的格式不正确"],
            pass: [/(.+){6,12}$/, '密码必须6到12位'],
            repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });
    form.on('submit(edit)', function (data) {
        data = data.field;
        data.hobby = getHobby().toLocaleString();
        data.id = sessionStorage.getItem("userId");
        //data.loginName=sessionStorage.getItem("loginName")
        let result=myAjax('http://localhost/ssm/back/user/edit_info',data,'post');
        if(result.code="500"){
            layer.alert("编辑失败", {
                icon: 1
            });
        }
        else{
            if(result.data="success") {
                layer.alert("编辑成功", {
                        icon: 6
                    },
                    function () {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            }else{
                layer.alert("用户名重复", {
                    icon: 1
                });
            }
        }
        return false;
    });
});
function  findById() {

    let id = sessionStorage.getItem("userId")
    let result =myAjax("http://localhost/ssm/back/user/findInfo",{id:id},'get')
    let data = result.data
    setUserData(data)
    /*let loginName=sessionStorage.getItem("loginName")
    $.ajax({

        url: "http://localhost/ssm/back/user/findInfo",
        data: {id:id,loginName:loginName},
        dataType: "json",
        type: "get",
        success:function (data) {
            if(data.result=="notLogin"){
                window.open("/html/login.html", "_top");
            }
            data=data.info;
            setUserData(data)

        }
    })*/

}

function setUserData(data){
    layui.use('form',function () {

        let form = layui.form;
        $("#L_username").val(data.username);
        $("#L_phone").val(data.mobile);
        $("#L_birthday").val(data.birthday);
        $("input[name=gender][value=" + data.gender + "]").attr("checked", true);

        if(data.hobby=='无'){

            $("input[name=hobby]").each(function () {

                $(this).prop("checked", false);

            })
        }

        let hobby = data.hobby.split(",");
        for (let j = 0; j < hobby.length; j++) {
            $("input[name=hobby]").each(function () {
                if (hobby[j] == $(this).val()) {
                    $(this).prop("checked", true);
                }
            })
        }
        form.render();
    })
}

function getHobby() {
    let value = [];

    $('input[name=hobby]:checked').each(function () {
        //这里用所选复选框或运算后的值
        value.push($(this).val());
    });
    if (value.length==0){
        value.push('无')
    }
    return value;
}
